package com.zzl.mongo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.bson.Document;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

@SuppressWarnings("resource")
public class MongoDBJDBC {
	
	public static void main( String args[] ){
	      try{   
	         // To connect to mongodb server
	         MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
//	         System.out.println(mongoClient);
	         // Now connect to your databases
	        /* DB db = mongoClient.getDB( "test" );
	         System.out.println("Connect to database successfully");
			boolean auth = db.authenticate("admin", "admin".toCharArray());
	         System.out.println("Authentication: "+auth);*/
	         
	         
	         MongoDatabase database = mongoClient.getDatabase("mycol");
	         
	         MongoCollection<Document> collection = database.getCollection("mycol");
	         
	                 Document doc = new Document("name", "MongoDB")
	                 .append("type", "database")
	                 .append("count", 1)
	                 .append("versions", Arrays.asList("v3.2", "v3.0", "v2.6"))
	                 .append("info", new Document("x", 203).append("y", 102));
	         
	         collection.insertOne(doc);
	         
	         List<Document> documents = new ArrayList<Document>();
	         for (int i = 0; i < 100; i++) {
	             documents.add(new Document("i", i));
	         }
	         
	         collection.insertMany(documents);
	         
	         System.out.println(collection.count());
	      }catch(Exception e){
	         System.err.println( e.getClass().getName() + ": " + e.getMessage() );
	      }
	   }
}
